#!/usr/bin/env bash

# config

___check_dir() {
  mkdir -p "${HOME}/.proxy"
}

___read_proxy_config() {
  if [ -d "${HOME}/.proxy" ]; then
    __PROXY_STATUS=$(cat "${HOME}/.proxy/status" 2>/dev/null)
    __PROXY_SOCKS5=$(cat "${HOME}/.proxy/socks5" 2>/dev/null)
    __PROXY_HTTP=$(cat "${HOME}/.proxy/http" 2>/dev/null)
    __PROXY_NO_PROXY=$(cat "${HOME}/.proxy/no_proxy" 2>/dev/null)
    __PROXY_GIT_PROXY_TYPE=$(cat "${HOME}/.proxy/git_proxy_type" 2>/dev/null)
  fi
}

___config_proxy() {
  echo "========================================"
  echo "ZSH Proxy Plugin Config"
  echo "----------------------------------------"

  echo -n "[socks5 proxy] {Default as 127.0.0.1:7890}
(address:port): "
  read -r __read_socks5

  echo -n "[http proxy]   {Default as 127.0.0.1:7890}
(address:port): "
  read -r __read_http

  echo -n "[no proxy domain] {Default as 'localhost,127.0.0.1,localaddress,.localdomain.com,.wen.cx,.k3x.cn'}
(comma separate domains): "
  read -r __read_no_proxy

  echo -n "[git proxy type] {Default as socks5}
(socks5 or http): "
  read -r __read_git_proxy_type
  echo "========================================"

  if [ -z "${__read_socks5}" ]; then
    __read_socks5="127.0.0.1:7890"
  fi
  if [ -z "${__read_http}" ]; then
    __read_http="127.0.0.1:7890"
  fi
  if [ -z "${__read_no_proxy}" ]; then
    __read_no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com,.wen.cx,.k3x.cn"
  fi
  if [ -z "${__read_git_proxy_type}" ]; then
    __read_git_proxy_type="socks5"
  fi

  ___check_dir
  echo "http://${__read_http}" >"${HOME}/.proxy/http"
  echo "socks5://${__read_socks5}" >"${HOME}/.proxy/socks5"
  echo "${__read_no_proxy}" >"${HOME}/.proxy/no_proxy"
  echo "${__read_git_proxy_type}" >"${HOME}/.proxy/git_proxy_type"

  ___read_proxy_config
}

# shell

___enable_proxy_all() {
  # http_proxy
  export http_proxy="${__PROXY_HTTP}"
  export HTTP_PROXY="${__PROXY_HTTP}"
  # https_proxy
  export https_proxy="${__PROXY_HTTP}"
  export HTTPS_PROXY="${__PROXY_HTTP}"
  # ftp_proxy
  export ftp_proxy="${__PROXY_HTTP}"
  export FTP_PROXY="${__PROXY_HTTP}"
  # rsync_proxy
  export rsync_proxy="${__PROXY_HTTP}"
  export RSYNC_PROXY="${__PROXY_HTTP}"
  # all_proxy
  export ALL_PROXY="${__PROXY_SOCKS5}"
  export all_proxy="${__PROXY_SOCKS5}"

  export no_proxy="${__PROXY_NO_PROXY}"
}

___disable_proxy_all() {
  unset http_proxy
  unset HTTP_PROXY
  unset https_proxy
  unset HTTPS_PROXY
  unset ftp_proxy
  unset FTP_PROXY
  unset rsync_proxy
  unset RSYNC_PROXY
  unset ALL_PROXY
  unset all_proxy
  unset no_proxy
}

___display_proxy_all() {
  echo ----------- Proxy Env ------------------
  echo
  echo "HTTP_PROXY   = ${HTTP_PROXY}"
  echo "HTTPS_PROXY  = ${HTTPS_PROXY}"
  echo "ALL_PROXY    = ${ALL_PROXY}"
  echo "FTP_PROXY    = ${FTP_PROXY}"
  echo "RSYNC_PROXY  = ${RSYNC_PROXY}"
  echo
  echo "http_proxy   = ${http_proxy}"
  echo "https_proxy  = ${https_proxy}"
  echo "all_proxy    = ${all_proxy}"
  echo "ftp_proxy    = ${ftp_proxy}"
  echo "rsync_proxy  = ${rsync_proxy}"
  echo
}

# Proxy for Git

___enable_proxy_git() {
  if [ "${__PROXY_GIT_PROXY_TYPE}" = "http" ]; then
    git config --global http.proxy "${__PROXY_HTTP}"
    git config --global https.proxy "${__PROXY_HTTP}"
  else
    git config --global http.proxy "${__PROXY_SOCKS5}"
    git config --global https.proxy "${__PROXY_SOCKS5}"
  fi
}

___disable_proxy_git() {
  git config --global --unset http.proxy
  git config --global --unset https.proxy
}

___display_proxy_git() {
  if command -v git >/dev/null; then
    echo
    echo ----------- GIT Proxy ------------------
    echo
    echo "GIT_PROXY = $(git config --global http.proxy)"
  fi
}

# NPM

___enable_proxy_npm() {
  if command -v npm >/dev/null; then
    npm config set proxy "${__PROXY_HTTP}"
    npm config set https-proxy "${__PROXY_HTTP}"
  fi
  if command -v yarn >/dev/null; then
    yarn config set proxy "${__PROXY_HTTP}" >/dev/null 2>&1
    yarn config set https-proxy "${__PROXY_HTTP}" >/dev/null 2>&1
  fi
}

___disable_proxy_npm() {
  if command -v npm >/dev/null; then
    npm config delete proxy
    npm config delete https-proxy
  fi
  if command -v yarn >/dev/null; then
    yarn config delete proxy >/dev/null 2>&1
    yarn config delete https-proxy >/dev/null 2>&1
  fi
}

___display_proxy_npm() {
  if command -v npm >/dev/null; then
    echo
    echo ----------- NPM Proxy ------------------
    echo
    echo "NPM_PROXY       = $(npm config get proxy)"
    echo "NPM_HTTPS_PROXY = $(npm config get https-proxy)"
  fi

  if command -v yarn >/dev/null; then
    echo
    echo ----------- YARN Proxy -----------------
    echo
    echo "YARN_PROXY       = $(yarn config get proxy)"
    echo "YARN_HTTPS_PROXY = $(yarn config get https-proxy)"
  fi
}

# Proxy for APT

___enable_proxy_apt() {
  if [ -d "/etc/apt/apt.conf.d" ]; then
    sudo touch /etc/apt/apt.conf.d/proxy.conf
    echo -e "Acquire::http::Proxy \"${__PROXY_HTTP}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf >/dev/null
    echo -e "Acquire::https::Proxy \"${__PROXY_HTTP}\";" | sudo tee -a /etc/apt/apt.conf.d/proxy.conf >/dev/null
    echo "- apt"
  fi
}

___disable_proxy_apt() {
  if [ -d "/etc/apt/apt.conf.d" ]; then
    sudo rm -rf /etc/apt/apt.conf.d/proxy.conf
  fi
}

___display_proxy_apt() {
  if command -v apt >/dev/null; then
    if [ -f "/etc/apt/apt.conf.d/proxy.conf" ]; then
      echo
      echo ----------- Apt Proxy ------------------
      echo
      cat /etc/apt/apt.conf.d/proxy.conf | sed 's/Acquire::http::Proxy/APT_HTTP_PROXY  =/g; s/Acquire::https::Proxy/APT_HTTPS_PROXY =/g; s/[";]//g;'
    fi
  fi
}

# info
___shell_dir=$(dirname $0)

___curl() {
  # -H "@${___shell_dir}/header"
  curl -H "@${___shell_dir}/header" --connect-timeout 3 -m 5 -sSL $@
}

___show_ip_inboard() {
  resp=$(___curl -4 -X POST -d 'ip=myip&accessKey=alibaba-inc' https://ip.taobao.com/outGetIpInfo 2>/dev/null)
  ip=$(echo $resp | grep -Eo '"ip":"([^"]*)"' | sed 's/"ip":"//g; s/"//g')
  city=$(echo $resp | grep -Eo '"city":"([^"]*)"' | sed 's/"city":"//g; s/"//g')
  country=$(echo $resp | grep -Eo '"country":"([^"]*)"' | sed 's/"country":"//g; s/"//g')
  result=$(printf "%-15s %s %s" $ip $city $country)
  echo "国内: ${result:-不通}"
}

___show_ip_outboard() {
  sbdata=$(___curl -4 https://api.ip.sb/geoip 2>/dev/null)
  ip=$(echo $sbdata | grep -Eo '"ip":"([^"]*)"' | sed 's/"ip":"//g; s/"//g')
  city=$(echo $sbdata | grep -Eo '"city":"([^"]*)"' | sed 's/"city":"//g; s/"//g')
  country=$(echo $sbdata | grep -Eo '"country":"([^"]*)"' | sed 's/"country":"//g; s/"//g')
  result=$(printf "%-15s %s %s" $ip $city $country)
  echo "国外: ${result:-不通}"
}

___show_ip_outwall() {
  result=$(___curl http://sspanel.net/ip.php 2>/dev/null | sed -e 's/<[^>]*>//g' | awk '{printf "%-15s %s %s %s", $1, $2, $3, $4}')
  echo "墙外: ${result:-不通}"
}

___show_ip_v6() {
  sbdata=$(___curl -6 https://api.ip.sb/geoip 2>/dev/null)
  ip=$(echo $sbdata | grep -Eo '"ip":"([^"]*)"' | sed 's/"ip":"//g; s/"//g')
  country=$(echo $sbdata | grep -Eo '"country":"([^"]*)"' | sed 's/"country":"//g; s/"//g')
  city=$(echo $sbdata | grep -Eo '"city":"([^"]*)"' | sed 's/"city":"//g; s/"//g')
  result=$(printf "%-15s %s %s" $ip $city $country)
  echo "IPV6: ${result:-不通}"
}

___enable_proxy() {
  ___enable_proxy_all
  ___enable_proxy_git
  ___enable_proxy_npm
  ___enable_proxy_apt
}

___disable_proxy() {
  ___disable_proxy_all
  ___disable_proxy_git
  ___disable_proxy_npm
  ___disable_proxy_apt
}

showip() {
  ___show_ip_inboard
  ___show_ip_outboard
  ___show_ip_outwall
  ___show_ip_v6
}

showp() {
  ___display_proxy_all
  ___display_proxy_git
  ___display_proxy_npm
  ___display_proxy_apt
  echo
  echo ----------- IP INFOMATION --------------
  echo
  showip
}

setp() {
  ___check_dir
  if [ -z "${__PROXY_STATUS}" ] || [ -z "${__PROXY_SOCKS5}" ] || [ -z "${__PROXY_HTTP}" ]; then
    ___config_proxy
  else
    echo "1" >"${HOME}/.proxy/status"
    ___enable_proxy
    showp
  fi
}

usetp() {
  ___check_dir
  echo "0" >"${HOME}/.proxy/status"
  ___disable_proxy
  showp
}

autop() {
  if [ -z "${__PROXY_STATUS}" ] || [ -z "${__PROXY_SOCKS5}" ] || [ -z "${__PROXY_HTTP}" ]; then
  else
    if [ "${__PROXY_STATUS}" = "1" ]; then
      ___enable_proxy
    fi
  fi
}

___read_proxy_config
autop
